package controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import pojo.User;
import service.UserManager;
import service.factory.UserManagerFactory;

@RestController
public class UserController {
	@Resource(name="userManager")
	private UserManager manager;
	
	@RequestMapping(value="/addUser", method=RequestMethod.POST,consumes="application/json")
	public User addUser(@RequestBody User user){
		System.out.println(user.getName()+"\t"+user.getSal()+"\t"+user.getBirth());
		return manager.addUser(user);
	}
	
	@RequestMapping(value="/findAll")
	public List<User> listAll(){
		List<User> users = manager.findAll();
		return users;
	}
	
	@RequestMapping(value="/deleteUser",method=RequestMethod.DELETE)
	public String deleteUser(String id){
		System.out.println("uid..."+id);
		int m=manager.deleteUser(id);
		String result="{}";
//		if(m>0){
//			result=id;
//		}else{
//			result="fail";
//		}
		return result;
	}
	
	@RequestMapping(value="/updateUser",method=RequestMethod.POST,consumes="application/json")
	public User updateUser(@RequestBody User user){
		System.out.println(user.getName()+"\t"+user.getSal()+"\t"+user.getBirth());
		int m=manager.updateUser(user);
//		if(m>0){
//			return u;
//		}else{
//			return new User();
//		}
		return user;
	}
}
